//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.MemberUse;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class MemberUseDao extends BaseDao<MemberUse, Integer> {
	public MemberUseDao() {
	}

	public void queryPageList(Pagination<MemberUse> pg, ConditionMap cd, ConditionMap cd2, String year) {
		ArrayList params = new ArrayList();
		String sqlw = cd.toSql(params);
		String sql = "select u.autoId, u.shopCode, u.memberCode, i.chName memberName, u.cardCode, u.contractCode, c.cardType, u.checkType, u.checkIp, u.checkTerminal, u.inTime, u.outTime, u.useCount, u.useBeforCount, u.useDate, u.createUser, u.createTime, u.updateTime\nfrom (\nselect autoId, shopCode, memberCode, cardCode, contractCode, checkType, checkIp, checkTerminal, inTime, outTime, useCount, useBeforCount, useDate, createUser, createTime, updateTime\nfrom MemberUse " + (sqlw.length() > 0?"where " + sqlw:"") + "\n";
		String sqlww;
		if(year == null || year.compareTo("2016") < 0) {
			sqlww = cd.toSql(params);
			sql = sql + "union all select autoId, shopCode, memberCode, cardCode, contractCode, checkType, checkIp, checkTerminal, inTime, outTime, useCount, useBeforCount, useDate, createUser, createTime, updateTime\nfrom MemberUse2015 " + (sqlww.length() > 0?"where " + sqlww:"") + "\n";
		}

		if(year == null || year.compareTo("2015") < 0) {
			sqlww = cd.toSql(params);
			sql = sql + "union all select autoId, shopCode, memberCode, cardCode, contractCode, checkType, checkIp, checkTerminal, inTime, outTime, useCount, useBeforCount, useDate, createUser, createTime, updateTime\nfrom MemberUseHistory " + (sqlww.length() > 0?"where " + sqlww:"") + "\n";
		}

		sqlww = cd2.toSql(params);
		sql = sql + ") u inner join MemberContract c on u.contractCode=c.contractCode\ninner join MemberInfo i on u.memberCode=i.memberCode\n" + (sqlww.length() > 0?"where " + sqlww + "\n":"") + "order by u.inTime desc";
		this.queryPageListBySql(MemberUse.class, pg, sql, params.toArray());
	}

	public MemberUse getTodayInByMemberCode(String shopCode, String memberCode) {
		return (MemberUse)this.getByWhere("shopCode=:p1 and memberCode=:p2 and inTime>:p3 and outTime is null", new Object[]{shopCode, memberCode, this.getTodayStartTime()});
	}

	public List<MemberUse> getTodayInList(String shopCode) {
		String sql = "select top 20 AutoId,mu.ShopCode,mu.MemberCode, mu.CardCode, ChName ContractCode,InTime,OutTime\nfrom MemberUse mu inner join MemberInfo mi on mu.memberCode=mi.memberCode\nwhere mu.shopCode=:p1 and inTime>:p2 and outTime is null order by InTime desc";
		return this.queryListBySql(MemberUse.class, sql, new Object[]{shopCode, this.getTodayStartTime()});
	}

	private Date getTodayStartTime() {
		Calendar cal = Calendar.getInstance();
		if(cal.get(11) < 3) {
			cal.add(11, -3);
		}

		cal.set(11, 3);
		cal.set(12, 0);
		return cal.getTime();
	}

	public void addUse(MemberUse use) {
		this.add(use);
		if(use.getUseCount().intValue() > 0) {
			String sql = "update MemberContract set restCount=restCount-:p1 where contractCode=:p2";
			this.updateBySql(sql, new Object[]{use.getUseCount(), use.getContractCode()});
		}

	}

	public void updateOut(MemberUse use) {
		this.update(use);
		if(use.getUseCount().intValue() > 1) {
			String sql = "update MemberContract set restCount=restCount-:p1 where contractCode=:p2";
			this.updateBySql(sql, new Object[]{Integer.valueOf(use.getUseCount().intValue() - 1), use.getContractCode()});
		}

	}
}
